﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmUsers : Form
    {
        DialogResult dlgResult;
        UsersInfo Usersinf;
        public frmUsers(int _Users_ID)
        {
            InitializeComponent();
            Usersinf = new UsersInfo();
            cboTask.SelectedIndex = 0;
            Usersinf.Users_ID = _Users_ID;
            if (_Users_ID > 0)
            {
                Usersinf = UsersDB.GetInfo(_Users_ID);
                InfoToForm();
            }
            btnSave.Enabled = Program.isAdmin;

            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            Usersinf.Users_Name = txtName.Text;
            Usersinf.Users_PassWord = txtPassWord.Text;
            Usersinf.Users_FullName = txtFullName.Text;
            Usersinf.Users_Address = txtAddress.Text;
            Usersinf.Users_Phone = txtPhone.Text;
            Usersinf.Users_Email = txtEmail.Text;
            Usersinf.Users_Roles = cboTask.Text;
        }
        private void InfoToForm()
        {
            txtName.Text = Usersinf.Users_Name;
            txtPassWord.Text = Usersinf.Users_PassWord;
            txtPassWordSalt.Text = Usersinf.Users_PassWord;
            txtFullName.Text = Usersinf.Users_FullName;
            txtAddress.Text = Usersinf.Users_Address;
            txtPhone.Text = Usersinf.Users_Phone;
            txtEmail.Text = Usersinf.Users_Email;
            cboTask.Text = Usersinf.Users_Roles;

        }
        private bool CheckValid()
        {
            bool retval = true;
            if (!txtPassWord.Text.Equals(txtPassWordSalt.Text))
            {
                txtPassWordSalt.Focus();
                Program.taskbarNotifier.Show(Message.Info, "Mật khẩu khẳng đinh không đúng!", UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                return false;
            }
            return retval;

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (Usersinf.Users_ID > 0)//Update
            {
                if (UsersDB.Exist(Usersinf.Users_ID, txtName.Text))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Exist, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    return;
                }
                if (UsersDB.Update(Usersinf))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Error, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    return;
                }
            }
            else //Create
            {
                if (UsersDB.Exist(Usersinf.Users_ID, txtName.Text))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Exist, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    return;
                }
                Usersinf.Users_ID = UsersDB.Insert(Usersinf);
                if (Usersinf.Users_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Error, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmUsers_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            Usersinf.Users_ID = 0;
            txtName.Text = "";
            txtPassWord.Text = "";
            txtPassWordSalt.Text = "";
            txtFullName.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            cboTask.Text = "";
            cboTask.SelectedIndex = 0;
        }

        private void frmUsers_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.F2:
                    btnSaveToNew_Click(null, null);
                    break;
                case Keys.Escape:
                    
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

        private void btnSaveToNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
            btnNew_Click(null, null);
        }

    }
}